<?php


namespace App\Http\Controllers\Admin\OrderManager;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

class InsideUserController extends BaseController
{
    /**
     * 获取内部账号列表信息
     * @param Request $request
     */
    public function list(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [

            ],
            [

            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $userCode = $request->input('user_code',0);
        $phone = $request->input('phone',0);
        $page = $request->input('page',1);
        $perPage = $request->input('perPage',20);

        $list = app('repo_inside_user')->list($userCode,$phone,$page,$perPage);
        return Response::sendData($list);
    }

    /**
     * 新增或修改
     * @param Request $request
     * @return string
     * @throws BasicException
     */
    public function addOrEdit(Request $request)
    {
        $param = $request->all();
        //参数校验
        $v = app('validator')->make($param, []);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $param['admin_id'] = $request->admin_id;

        $res = app('repo_inside_user')->addOrEdit($param);
        return Response::sendData($res,'保存成功');
    }

    /**
     * 获取用户信息
     * @param Request $request
     * @return string
     * @throws BasicException
     */
    public function userInfo(Request $request)
    {
        $params = $request->only('user_code');
        $v = app('validator')->make($params, [
            'user_code' => 'required|integer|min:1|max:2100000000'
        ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }

        $userCode = $params['user_code'];

        //查询用户信息
        $userData = app('model_user_data')->getInfoByUserCode($userCode,['ud.user_code', 'ud.nickname', 'u.mobile','u.user_id']);
        return Response::sendData($userData);
    }

    /**
     * 删除内部账号信息
     * @param Request $request
     * @throws BasicException
     */
    public function delete(Request $request)
    {
        $params = $request->only('ids');
        $v = app('validator')->make($params, [

        ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }

        $idArr = explode(",", $params['ids']);
        $res = app('repo_inside_user')->deleteInside($idArr);

        return Response::sendData($res,'删除成功');
    }

}